package tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

public class CustomTag3WithField extends TagSupport {
    private static final long serialVersionUID = 1391214075111235285L;
    private String url;

    private String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        String username = req.getParameter("username");
        if (username != null && !username.equals("")) {
            try {
                out.print("你输入的username为：" + username + "<br>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                out.print("你没有为参数username指定值<br>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        String username = req.getParameter("username");
        if (username != null && !username.equals("")) {
            try {
                out.print("欢迎访问此页！");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                out.print("<a href='" + this.getUrl() + "'>点击这里</a>重新提交表单");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return EVAL_PAGE;
    }
}
